package org.gdstash.ui.util;

import javax.swing.*;
import java.util.Vector;

public class EnabledComboBoxModel<E> extends DefaultComboBoxModel<E> {
    private Vector<Boolean> enabled;

    public EnabledComboBoxModel() {
        this.enabled = new Vector<Boolean>();
    }

    public EnabledComboBoxModel(final E[] items) {
        super(items);
        this.enabled = new Vector<Boolean>();
    }

    public EnabledComboBoxModel(final Vector<E> v) {
        super(v);
        this.enabled = new Vector<Boolean>();
    }

    @Override
    public void addElement(final E anObject) {
        this.addElement(anObject, true);
    }

    public void addElement(final E anObject, final boolean enabled) {
        Boolean b = null;
        if (enabled) {
            b = Boolean.TRUE;
        } else {
            b = Boolean.FALSE;
        }
        this.enabled.add(b);
        super.addElement(anObject);
    }

    @Override
    public void insertElementAt(final E anObject, final int index) {
        this.insertElementAt(anObject, index, true);
    }

    public void insertElementAt(final E anObject, final int index, final boolean enabled) {
        super.insertElementAt(anObject, index);
        Boolean b = null;
        if (enabled) {
            b = Boolean.TRUE;
        } else {
            b = Boolean.FALSE;
        }
        this.enabled.add(index, b);
    }

    @Override
    public void removeAllElements() {
        super.removeAllElements();
        this.enabled.clear();
    }

    @Override
    public void removeElement(final Object anObject) {
        final int index = super.getIndexOf(anObject);
        if (index == -1) {
            return;
        }
        super.removeElement(anObject);
        this.enabled.remove(index);
    }

    @Override
    public void removeElementAt(final int index) {
        super.removeElementAt(index);
        this.enabled.remove(index);
    }

    @Override
    public void setSelectedItem(final Object anObject) {
        final int index = super.getIndexOf(anObject);
        if (index == -1) {
            return;
        }
        if (!this.isEnabled(index)) {
            return;
        }
        super.setSelectedItem(anObject);
    }

    public boolean isEnabled(final int index) {
        Boolean b = null;
        try {
            b = this.enabled.get(index);
        } catch (ArrayIndexOutOfBoundsException ex) {
            return false;
        }
        return b != null && b.equals(Boolean.TRUE);
    }

    public void setEnabled(final boolean enabled, final int index) {
        Boolean b = null;
        if (enabled) {
            b = Boolean.TRUE;
        } else {
            b = Boolean.FALSE;
        }
        this.enabled.set(index, b);
    }

    public void setFirstEnabledIndex() {
        final int size = this.getSize();
        int index = -1;
        for (int i = 0; i < size; ++i) {
            if (this.isEnabled(i)) {
                index = i;
                break;
            }
        }
        this.setSelectedItem(this.getElementAt(index));
    }
}
